const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
const formatTime_chinese_m = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return  year + "年" + formatNumber(month) + "月"+formatNumber(day)+"日" + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
const formatDate = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  return [year, month, day].map(formatNumber).join('-')
}

const formatDate_chinese = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return year + "年" + formatNumber(month) + "月"+formatNumber(day)+"日"
}
const formatDate_chinese_monthdate = date => {
  const month = date.getMonth() + 1
  const day = date.getDate()
  return month + "月" + day + "日"
}
const formatTime_chinese = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  // const second = date.getSeconds()

  return year + "年" + month + "月" + day + "日" + ' ' + [hour, minute].map(formatNumber).join(':')
}
const formatTime_e = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  // const second = date.getSeconds()

  return year + "-" + month + "-" + day +  ' ' + [hour, minute].map(formatNumber).join(':')
}
const formatTime_chineseTime = date => {
  const hour = date.getHours()
  const minute = date.getMinutes()
  // const second = date.getSeconds()

  return  [hour, minute].map(formatNumber).join(':')
}


const formatTime_emotion = date => {
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  // const second = date.getSeconds()

  return month + "月" + day + '日 ' + [hour, minute].map(formatNumber).join(':')
}
//比较微信版本号
function compareDate(v1, v2) {//两个日期相差几天
  let tmp = (Date.parse(v1.toLocaleDateString()) - Date.parse(v2.toLocaleDateString())) / (24 * 60 * 60 * 1000);
  return tmp;
}
const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
//比较微信版本号
function compareVersion(v1, v2) {
  v1 = v1.split('.')
  v2 = v2.split('.')
  const len = Math.max(v1.length, v2.length)

  while (v1.length < len) {
    v1.push('0')
  }
  while (v2.length < len) {
    v2.push('0')
  }

  for (let i = 0; i < len; i++) {
    const num1 = parseInt(v1[i])
    const num2 = parseInt(v2[i])

    if (num1 > num2) {
      return 1
    } else if (num1 < num2) {
      return -1
    }
  }

  return 0
}
// 获取每周周一
function getWeeklyMonday(time) {
  let dd = ''
  if (time) {
    dd = new Date(time)
  } else {
    dd = new Date()
  }
  var week = dd.getDay() // 获取时间的星期数
  var minus = week ? week - 1 : 6
  dd.setDate(dd.getDate() - minus) // 获取minus天前的日期
  var y = dd.getFullYear()
  var m = dd.getMonth() + 1 // 获取月份
  var d = dd.getDate()
  return new Date(y + '-' + m + '-' + d)
  // return new Date(y + '-' + m + '-' + d).getTime() / 1000
}
//判断字符是否为空的方法
function strIsEmpty(obj) {
  var regu = "^[ ]+$";
  var re = new RegExp(regu);
  if (typeof obj == "undefined" || obj == null || obj == "" || re.test(obj)) {
    return true;
  } else {
    return false;
  }
}
function detailUri(query)
{
  var arr = query.split('&')
  var theRequest = new Object();
    for (var i = 0; i < arr.length; i++) {
        var kye = arr[i].split("=")[0]
        var value = arr[i].split("=")[1]
        // 给对象赋值
        theRequest[kye] = value
    }
  return theRequest;
}
function videoTime(duration){//time是秒数
  let secondTime = parseInt(duration);// 秒
  let minuteTime = 0;// 分
  let hourTime = 0;// 小时
      if (secondTime > 60) { // 如果秒数大于60，将秒数转换成整数
        // 获取分钟，除以60取整数，得到整数分钟
        minuteTime = parseInt(secondTime / 60);
        // 获取秒数，秒数取佘，得到整数秒数
        secondTime = parseInt(secondTime % 60)
        // 如果分钟大于60，将分钟转换成小时
        if (minuteTime > 60) {
          // 获取小时，获取分钟除以60，得到整数小时
          hourTime = parseInt(minuteTime / 60);
          // 获取小时后取佘的分，获取分钟除以60取佘的分
          minuteTime = parseInt(minuteTime % 60);
        }
  }
      let result = secondTime > 9 ? secondTime : ('0' + secondTime);
      if (minuteTime > 0) {
        result = (minuteTime > 9 ? minuteTime : ('0' + minuteTime)) + ":" + result;
      } else {
        result = "00" + ":" + result;
      }    
      if (hourTime > 0) {
        result = (hourTime > 9 ? hourTime : ('0' + hourTime)) + ":" + result;
      } else {
        result = result;
      }
      return result;
}

function formatvoiceTime(time) {
  if (typeof time !== 'number' || time < 0) {
    return time
  }
 
  var hour = parseInt(time / 3600)
  time = time % 3600
  var minute = parseInt(time / 60)
  time = time % 60
  var second = time
 
  return ([hour, minute, second]).map(function (n) {
    n = n.toString()
    return n[1] ? n : '0' + n
  }).join(':')
}
 
function formatLocation(longitude, latitude) {
  if (typeof longitude === 'string' && typeof latitude === 'string') {
    longitude = parseFloat(longitude)
    latitude = parseFloat(latitude)
  }
 
  longitude = longitude.toFixed(2)
  latitude = latitude.toFixed(2)
 
  return {
    longitude: longitude.toString().split('.'),
    latitude: latitude.toString().split('.')
  }
}

function times(time){
  var datas = new Date().toLocaleDateString()
  var dd =Date.parse(datas)/1000
  var dates =Math.round(new Date(time).getTime()/1000).toString();
  var iday = Math.floor(parseInt(dd-Number(dates))/60/60/24)
  //  -1 今天 0 昨天  1更早之前
   return iday
}

function timesHour(time){
  var datas = new Date()
  var dd =Date.parse(datas)/1000
  var dates =Math.round(new Date(time).getTime()/1000);
  var ihour = (dd-Number(dates))/60
  return ihour
}
module.exports = {
  formatTime: formatTime,
  formatDate: formatDate,
  formatDate_chinese: formatDate_chinese,
  formatDate_chinese_monthdate: formatDate_chinese_monthdate,
  compareVersion: compareVersion,
  formatTime_chinese: formatTime_chinese,
  formatTime_emotion: formatTime_emotion,
  compareDate: compareDate,
  getWeeklyMonday: getWeeklyMonday,
  strIsEmpty: strIsEmpty,
  detailUri:detailUri,
  videoTime:videoTime,
  formatvoiceTime:formatvoiceTime,
  formatLocation:formatLocation,
  times:times,
  formatTime_chineseTime:formatTime_chineseTime,
  timesHour:timesHour,
  formatTime_e:formatTime_e,
  formatTime_chinese_m:formatTime_chinese_m
}
